package com.skyward.android.androiddemo2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddLayout extends AppCompatActivity {
    TextView descriptionView;
    View expandView;
    int maxDescripLine = 3;
    private LinearLayout test_layout;
    boolean isExpand = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_layout);

        descriptionView = (TextView)findViewById(R.id.description_view);
        test_layout = (LinearLayout) findViewById(R.id.test_layout);
        expandView = findViewById(R.id.expand_view);
        descriptionView.setText(getText(R.string.content));
        descriptionView.setHeight(descriptionView.getLineHeight() * maxDescripLine);
        descriptionView.post(new Runnable() {

            @Override
            public void run() {
                expandView.setVisibility(descriptionView.getLineCount() > maxDescripLine ? View.VISIBLE : View.GONE);

            }
        });

//        findViewById(R.id.description_layout).setOnClickListener(new View.OnClickListener() {
//            boolean isExpand;
//
//            @Override
//            public void onClick(View v) {
//                isExpand = !isExpand;
//                descriptionView.clearAnimation();
//                final int deltaValue;
//                final int startValue = descriptionView.getHeight();
//                int durationMillis = 350;
//                if (isExpand) {
//                    deltaValue = descriptionView.getLineHeight() * descriptionView.getLineCount() - startValue;
//                    RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//                    animation.setDuration(durationMillis);
//                    animation.setFillAfter(true);
//                    expandView.startAnimation(animation);
//                } else {
//                    deltaValue = descriptionView.getLineHeight() * maxDescripLine - startValue;
//                    RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//                    animation.setDuration(durationMillis);
//                    animation.setFillAfter(true);
//                    expandView.startAnimation(animation);
//                }
//                Animation animation = new Animation() {
//                    protected void applyTransformation(float interpolatedTime, Transformation t) {
//                        descriptionView.setHeight((int) (startValue + deltaValue * interpolatedTime));
//                    }
//
//                };
//                animation.setDuration(durationMillis);
//                descriptionView.startAnimation(animation);
//            }
//        });

        findViewById(R.id.description_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                test_layout.clearAnimation();
                final int deltaValue;
                final int startValue = test_layout.getHeight();
                int durationMillis = 350;
                if(isExpand){
                    deltaValue = 600;

                    isExpand = false;
                    Log.i("myLog","打开");
                }else {
                    deltaValue = -600;
                    isExpand = true;
                    Log.i("myLog","收起");

                }
                Animation animation = new Animation() {
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) test_layout.getLayoutParams();
                        layoutParams.height = (int) (startValue + deltaValue * interpolatedTime);
                        test_layout.setLayoutParams(layoutParams);
                    }
                };
                animation.setDuration(durationMillis);
                test_layout.setAnimation(animation);


            }
        });
    }
}
